package cn.ml.mybatisdemo.servlet;

import cn.ml.mybatisdemo.dao.PersonDao;
import cn.ml.mybatisdemo.entity.Person;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class SavePersonServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        String sex = req.getParameter("sex");
        Person person = new Person();
        person.setName(name);
        person.setSex(sex);
        PersonDao personDao = ((SqlSession) req.getAttribute("sqlSession")).getMapper(PersonDao.class); //从过滤器中取出sqlSession
        personDao.save(person);
        req.setAttribute("person",person);
        req.getRequestDispatcher("/WEB-INF/saveok.jsp").forward(req,resp);//转发
    }
}
